package cn.ht.jywrwater.module.ht.service.watersurvey;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.net.InetAddress;
import java.util.*;
import java.util.stream.Collectors;

import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.watersurvey.WaterSurveyDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.watersurvey.WaterSurveyConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.watersurvey.WaterSurveyMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 运行监测概览 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterSurveyServiceImpl implements WaterSurveyService {

    @Resource
    private WaterSurveyMapper waterSurveyMapper;

    @Override
    public Long createWaterSurvey(WaterSurveyCreateReqVO createReqVO) {
        // 插入
        WaterSurveyDO waterSurvey = WaterSurveyConvert.INSTANCE.convert(createReqVO);
        waterSurveyMapper.insert(waterSurvey);
        // 返回
        return waterSurvey.getId();
    }

    @Override
    public void updateWaterSurvey(WaterSurveyUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterSurveyExists(updateReqVO.getId());
        // 更新
        WaterSurveyDO updateObj = WaterSurveyConvert.INSTANCE.convert(updateReqVO);
        waterSurveyMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterSurvey(Long id) {
        // 校验存在
        validateWaterSurveyExists(id);
        // 删除
        waterSurveyMapper.deleteById(id);
    }

    private void validateWaterSurveyExists(Long id) {
        if (waterSurveyMapper.selectById(id) == null) {
            throw exception(WATER_SURVEY_NOT_EXISTS);
        }
    }

    @Override
    public WaterSurveyDO getWaterSurvey(Long id) {
        return waterSurveyMapper.selectById(id);
    }

    @Override
    public List<WaterSurveyDO> getWaterSurveyList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterSurveyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterSurveyDO> getWaterSurveyPage(WaterSurveyPageReqVO pageReqVO) {
        return waterSurveyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterSurveyDO> getWaterSurveyList(WaterSurveyExportReqVO exportReqVO) {
        return waterSurveyMapper.selectList(exportReqVO);
    }

    @Override
    public List<WaterSurveyTypeListVO> getAllByType() {
        List<WaterSurveyTypeListVO> waterSurveyTypeVOList = new ArrayList<>();
        List<WaterSurveyDO> allWaterSurveyList = waterSurveyMapper.selectList();
        List<Integer> allTypeList = allWaterSurveyList.stream().map(WaterSurveyDO::getType).distinct().collect(Collectors.toList());
        for (Integer type:allTypeList){
            WaterSurveyTypeListVO waterSurveyTypeVO = new WaterSurveyTypeListVO();
            waterSurveyTypeVO.setType(type);
            List<WaterSurveyDO> allWaterTypeSurveyList = waterSurveyMapper.selectList("type", type);
            waterSurveyTypeVO.setWaterSurveyList(allWaterTypeSurveyList);
            // 在线数量统计
            int online = (int) allWaterTypeSurveyList.stream().filter(waterSurveyDO -> waterSurveyDO.getStatus() == true).count();
            int offline = (int) allWaterTypeSurveyList.stream().filter(waterSurveyDO -> waterSurveyDO.getStatus() == false).count();
            waterSurveyTypeVO.setOnline(online);
            waterSurveyTypeVO.setCount(allWaterTypeSurveyList.size());
            waterSurveyTypeVOList.add(waterSurveyTypeVO);
        }
        return waterSurveyTypeVOList;
    }

    @Override
    public List<WaterSurveyTypeListVO> getByType(Collection<Integer> types) {
        List<WaterSurveyTypeListVO> responseVOList = new ArrayList<>();
        for (Integer type:types){
            WaterSurveyTypeListVO waterSurveyTypeVO =new WaterSurveyTypeListVO();
            List<WaterSurveyDO> waterTypeSurveyDOList = waterSurveyMapper.selectList("type", type);
             waterSurveyTypeVO.setType(type);
            waterSurveyTypeVO.setWaterSurveyList(waterTypeSurveyDOList);
            responseVOList.add(waterSurveyTypeVO);
        }
        return responseVOList;
    }

    @Override
    public WaterSurveyCountVO countByTypeAndStatus(Integer type, Integer status) {
        List<WaterSurveyDO> waterTypeSurveyStatusList = new ArrayList<>();
        List<WaterSurveyDO> waterTypeSurveyList = waterSurveyMapper.selectList("type", type);
        if (status==1){
            for (WaterSurveyDO waterSurvey:waterTypeSurveyList){
                if (waterSurvey.getStatus()){
                    waterTypeSurveyStatusList.add(waterSurvey);
                }
            }
        }
        if (status==0){
            for (WaterSurveyDO waterSurvey:waterTypeSurveyList){
                if (!waterSurvey.getStatus()){
                    waterTypeSurveyStatusList.add(waterSurvey);
                }
            }
        }
        WaterSurveyCountVO result = new WaterSurveyCountVO();
        result.setStatus(status);
        result.setType(type);
        result.setNums(waterTypeSurveyStatusList.size());

        return result;
    }

}
